#include "../include/file_transfer.h"
#include <iostream>
#include "../include/conf_reader.h"
#include <nlohmann/json.hpp>
#include "../include/logical_handle.h"
file_transfer_thread_item::file_transfer_thread_item()
{
    this->is_running=true;
    _thread=std::thread([this](){
        
        while(1){

            std::unique_lock<std::mutex> u_l(this->mtx);
            this->cond_var.wait(u_l,[this](){
                return !this->task_queue.empty()||!is_running;
            });

            if(is_running==false){
                std::cout<<"一个文件传输线程即将关闭"<<std::endl;
                break;
            }
            auto head=this->task_queue.front();
            this->task_queue.pop_front();

            u_l.unlock();

            //执行任务
            this->doing_task(head);
            delete head;

            
        }

    });

}


void file_transfer_thread_item::add_task(struct full_msg * task_item)
{
    {
        std::lock_guard<std::mutex> l_g(mtx);
        this->task_queue.push_back(task_item);
        this->cond_var.notify_one();
    }

}

file_transfer_thread_item::~file_transfer_thread_item()
{
    is_running=false;
    this->cond_var.notify_all();
    this->_thread.join();

    //判断是否还有任务
    if(!this->task_queue.empty()){
        std::cout<<"一个线程还有任务没执行玩"<<std::endl;
        //可以考虑去执行
        while(!task_queue.empty()){
            auto head=this->task_queue.front();
            this->task_queue.pop_front();
            doing_task(head);
            delete head;
        }
    }
    std::cout<<"成功关闭一个线程"<<std::endl;


}



void file_transfer_thread_item::doing_task(struct full_msg * msg)
{
    //根据类型处理
    if(msg->message_id==recv_package_id::FILE_SEND_APPLY){
        std::cout<<"收到 FILE_SEND_APPLY 包"<<std::endl;
        logical_handle::get_instance()->file_send_apply(this->file_map,msg);
    }else if(msg->message_id==recv_package_id::FILE_SLICE){
        std::cout<<"收到 FILE_SLICE 包"<<std::endl;
        logical_handle::get_instance()->file_slice_send(this->file_map,msg);
    }else if(msg->message_id==recv_package_id::FILE_DOWNLOAD_APPLY){
        std::cout<<"收到 FILE_DOWNLOAD_APPLY 包"<<std::endl;
        logical_handle::get_instance()->file_download_apply(read_file_map,msg);
    }
    else{
        BOOST_LOG_TRIVIAL(error)<<"居然没有对应的处理函数！！！";
    }
    

}




//---------------------------pool--------------------------------


void file_transfer_thread_pool::deal_task(struct full_msg * msg)
{
    std::string hash_file_id=msg->req_json["file_hash_id"].get<std::string>();
    std::hash<std::string> hash_fn;
    size_t hash_value = hash_fn(hash_file_id);
    int thread_id=hash_value%pool_num;

    //放入即可
    pool[thread_id]->add_task(msg);
    
    
    

}







file_transfer_thread_pool::file_transfer_thread_pool(){
    this->pool_num=atoi(conf_reader::get_conf_reader_instance()->get_value_from_key("file_transfer_pool_num","3"));
    for(int i=0;i<pool_num;++i){
        std::shared_ptr<file_transfer_thread_item> tmp_item=std::make_shared<file_transfer_thread_item>();
        this->pool.push_back(tmp_item);
    }

}

file_transfer_thread_pool::~file_transfer_thread_pool()
{
    this->pool.clear();
}